package com.zsc.controller.common;

import com.zsc.dto.UserLoginDTO;
import com.zsc.entity.User;
import com.zsc.exception.BaseException;
import com.zsc.result.Result;
import com.zsc.service.UserService;
import com.zsc.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@RestController("commonUserController")
@Slf4j
@Api(tags = "通用端 用户管理")
public class UserController {

    @Autowired
    UserService userservice;


    @ApiOperation(value = "用户登录")
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("{}", userLoginDTO);
        return Result.success(userservice.login(userLoginDTO));
    }

    /**
     * 退出
     *
     * @return
     */
    @ApiOperation(value = "退出")
    @GetMapping("/logout")
    public Result<String> logout() {
        return Result.success();
    }


    /**
     * 用户注册
     * @param user
     * @return
     */
    @ApiOperation(value = "用户注册")
    @PostMapping("/user/register")
    public Result<String> register(@RequestBody User user) {
        try {
            if (Objects.isNull(user)) {
                throw new BaseException("用户不能为空");
            }
            userservice.save(user);
            return Result.success("注册成功");
        } catch (BaseException e) {
            return Result.error("注册失败！！！！");
        }
    }
}
